package com.example.demo.generator.codegen.util;

/**
 * Case-insensitive string helper mirroring the behaviour from SpringBootCodeGenerator.
 */
public final class NonCaseString implements CharSequence {

    /**
     * 原始字符串内容。
     */
    private final String value;

    private NonCaseString(String value) {
        if (value == null) {
            throw new IllegalArgumentException("value must not be null");
        }
        this.value = value;
    }

    public static NonCaseString of(String str) {
        return new NonCaseString(str);
    }

    public int indexOf(String m) {
        if (m == null || value.length() < m.length()) {
            return -1;
        }
        return value.toLowerCase().indexOf(m.toLowerCase());
    }

    public int lastIndexOf(String m) {
        if (m == null || value.length() < m.length()) {
            return -1;
        }
        return value.toLowerCase().lastIndexOf(m.toLowerCase());
    }

    public boolean contains(String m) {
        if (m == null || value.length() < m.length()) {
            return false;
        }
        return value.toLowerCase().contains(m.toLowerCase());
    }

    public boolean containsAny(String... matchers) {
        if (matchers == null) {
            return false;
        }
        for (String matcher : matchers) {
            if (contains(matcher)) {
                return true;
            }
        }
        return false;
    }

    public NonCaseString trim() {
        return of(value.trim());
    }

    public NonCaseString replace(char oldChar, char newChar) {
        return of(value.replace(oldChar, newChar));
    }

    public NonCaseString replaceAll(String regex, String replacement) {
        return of(value.replaceAll(regex, replacement));
    }

    public NonCaseString substring(int beginIndex) {
        return of(value.substring(beginIndex));
    }

    public NonCaseString substring(int beginIndex, int endIndex) {
        return of(value.substring(beginIndex, endIndex));
    }

    public boolean isNotEmpty() {
        return !value.isEmpty();
    }

    public String[] split(String regex) {
        return value.split(regex);
    }

    public boolean containsAllIgnoreCase(String... matchers) {
        if (matchers == null) {
            return false;
        }
        for (String matcher : matchers) {
            if (!contains(matcher)) {
                return false;
            }
        }
        return true;
    }

    public String get() {
        return value;
    }

    @Override
    public int length() {
        return value.length();
    }

    @Override
    public char charAt(int index) {
        return value.charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
        return value.subSequence(start, end);
    }

    @Override
    public String toString() {
        return value;
    }
}
